package com.cenwangkeji.resident.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.SignBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.presenter.activity.SignPresenter;
import com.cenwangkeji.resident.ui.adapter.SignAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.utils.common.SharePreferenceUtils;
import com.rxjava.rxlife.RxLife;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

//签约界面
public class SignActivity extends BaseActivity implements SignPresenter.I, BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.rv)
    RecyclerView mRv;
    private SignAdapter mAdapter;
    private SignPresenter mPresenter = new SignPresenter(this);


    @Override
    public int setLayoutId() {
        return R.layout.activity_sign;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        showLoading();
        initRecyelerView();
        mPresenter.getSign();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initRecyelerView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SignAdapter(null);
        mAdapter.setOnItemChildClickListener(this);
        mRv.setAdapter(mAdapter);


    }

    @Override
    public void onSignSuccess(SignBean bean) {
        if (bean.getMsg().equals("Token已过期")) {
            SharePreferenceUtils.putBoolean(this, Constant.is_login, false);//登录标识
            startActivity(new Intent(this, LoginActivity.class));
            MyToast.show("登录过期，请重新登录");
            finish();
        } else {
            if (bean.getData() != null) {
                mAdapter.replaceData(bean.getData());
            } else {
                MyToast.show(bean.getMsg());
            }
        }
        dismissLoading();
    }
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        send();

    }

    private void send(){
        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("mark", 3);
                map.put("userId", User.getUserId());
                map.put("token", User.getToken());
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.sms_sign);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, SuccessBean.class));

            }
        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
            @Override
            public void onNext(SuccessBean bean) {
                if (bean.getCode() == 0) {
                    MyToast.show("短信发送成功");
                    new MaterialDialog.Builder(SignActivity.this)
                            .title("续约")
                            .content("")
                            .inputType(InputType.TYPE_CLASS_NUMBER)
                            .input("填写短信验证码", null, new MaterialDialog.InputCallback() {
                                @Override
                                public void onInput(MaterialDialog dialog, CharSequence input) {
                                    if (TextUtils.isEmpty(input)) {
                                        MyToast.show("请输入验证码");
                                    } else {
                                        sign(input+"");
                                    }
                                }
                            })
                            .positiveText("确定")
                            .show();
                } else {
                    MyToast.show(bean.getMsg());
                }
            }
        });
    }
    private void sign(final String sms){
        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("mark", 3);
                map.put("userId", User.getUserId());
                map.put("token", User.getToken());
                map.put("sms", sms);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.sign);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, SuccessBean.class));

            }
        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
            @Override
            public void onNext(SuccessBean bean) {
                if (bean.getCode() == 0) {
                    MyToast.show("签约成功");
                    mPresenter.getSign();
                } else {
                    MyToast.show(bean.getMsg());
                }
            }
        });
    }
}
